#include "main.h"
#include "system.h"

TIM_HandleTypeDef Tim1Handle;
void SysTimeInitial(void)
{
  /*****************************
  	HAL_RCC_GetPCLK1Freq=  48000000
	��ʱ���ж�����=PCLK/(Prescaler+1)(Period+1)
  *****************************/
  Tim1Handle.Instance = TIM1;
  Tim1Handle.Init.Prescaler = 4800 - 1;
  Tim1Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
  Tim1Handle.Init.Period = 10 - 1;
  Tim1Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  Tim1Handle.Init.RepetitionCounter = 0;
  �Զ���װ�ؼ�������ֵ���� ARPE λ�� 0 ʱ���Զ���װ�ؼ�������ֵ��������Ч
  Tim1Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&Tim1Handle) != HAL_OK)
  {
    Error_Handler();
  }

  HAL_TIM_Base_Start_IT(&Tim1Handle);

}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base)
{
  GPIO_InitTypeDef GPIO_InitStruct;	
  if (htim_base->Instance == TIM1)
  {
    HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn,2, 0);
    HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
    __HAL_RCC_TIM1_CLK_ENABLE();
  }
  else if (htim_base->Instance == TIM3)
  {
  	  __HAL_RCC_TIM3_CLK_ENABLE();
	  /*GPIO_InitStruct.Pin 	= 	GPIO_PIN_6;
	  GPIO_InitStruct.Mode 	= 	GPIO_MODE_AF_PP;
	  GPIO_InitStruct.Speed = 	GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);*/
  }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base)
{
  if (htim_base->Instance == TIM1)
  {
    HAL_NVIC_DisableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
	__HAL_RCC_TIM1_CLK_DISABLE();
  }
  else if (htim_base->Instance == TIM3)
  {
	__HAL_RCC_TIM3_CLK_DISABLE();
  }
}

uint8 	Flag=0;
uint16	Sys_Cnt=0;
uint16	BuzzerCnt=0;
uint32  Sys_Rest_Cnt=0;	
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	
  if (htim->Instance == TIM1)
  {
  	++Sys_Cnt;
	++Sys_Rest_Cnt;
	
	
		
	
	if(KeyStatu.Key_Int==Menu_Key_Detct)��μ��ȥ����
	{
		if (Menu_Key_Status == 0)
		{
			++KeyStatu.Menu_Key_Low_Time;
		}
		else 
		{
			if(KeyStatu.Menu_Key_Low_Time>=2000)
			{
				����
				KeyStatu.Menu_Key_Low_Time=0;
				KeyStatu.Menu_Key_LongPress=1;
				KeyStatu.Menu_Key_ShortPress=0;
				KeyStatu.Key_Int=0;
				KeyStatu.BuzzerFlag=1;
				BUZZER_ON;
				BuzzerCnt=0;
			}
			else if(KeyStatu.Menu_Key_Low_Time>=100)��0.1s
			{
				�̰�
				KeyStatu.Menu_Key_Low_Time=0;
				KeyStatu.Menu_Key_LongPress=0;
				KeyStatu.Menu_Key_ShortPress=1;
				KeyStatu.Key_Int=0;
				KeyStatu.BuzzerFlag=1;
				BUZZER_ON;
				BuzzerCnt=0;
			}
			else
			{
				����
				KeyStatu.Menu_Key_Low_Time=0;
				KeyStatu.Menu_Key_LongPress=0;
				KeyStatu.Menu_Key_ShortPress=0;
				KeyStatu.Key_Int=0;
			}
		
		}
		
		if(KeyStatu.Menu_Key_Low_Time>=2000)
		{
			����
			KeyStatu.Menu_Key_Low_Time=0;
			KeyStatu.Menu_Key_LongPress=1;
			KeyStatu.Menu_Key_ShortPress=0;
			KeyStatu.Key_Int=0;
			KeyStatu.BuzzerFlag=1;
			BUZZER_ON;
			BuzzerCnt=0;			
		}	
	}

	if(KeyStatu.Key_Int==Move_Key_Detct)
	{
		if (Move_Key_Status == 0)
		{
			++KeyStatu.Move_Key_Low_Time;
		}
		else 
		{
			if(KeyStatu.Move_Key_Low_Time>=100)��0.1s
			{
				KeyStatu.Move_Key_Low_Time=0;
				KeyStatu.Move_Key_Press=1;
				KeyStatu.Key_Int=0;
				KeyStatu.BuzzerFlag=1;
				BUZZER_ON;
				BuzzerCnt=0;				
			}
			else
			{
				KeyStatu.Move_Key_Low_Time=0;
				KeyStatu.Move_Key_Press=0;
				KeyStatu.Key_Int=0;
			}
		
		}
		
		if(KeyStatu.Move_Key_Low_Time>=100)
		{
			KeyStatu.Move_Key_Low_Time=0;
			KeyStatu.Move_Key_Press=1;
			KeyStatu.Key_Int=0;
			KeyStatu.BuzzerFlag=1;
			BUZZER_ON;
			BuzzerCnt=0;			
		}	

	}
	
	if(KeyStatu.Key_Int==Selet_Key_Detct)
	{
		if (Selet_Key_Status== 0)
		{
			++KeyStatu.Select_Key_Low_Time;
		}
		else 
		{
			if(KeyStatu.Select_Key_Low_Time>=100)��0.1s
			{
				KeyStatu.Select_Key_Low_Time=0;
				KeyStatu.Select_Key_Press=1;
				KeyStatu.Key_Int=0;
				KeyStatu.BuzzerFlag=1;
				BUZZER_ON;
				BuzzerCnt=0;				
			}
			else
			{
				KeyStatu.Select_Key_Low_Time=0;
				KeyStatu.Select_Key_Press=0;
				KeyStatu.Key_Int=0;
			}
		
		}
		
		if(KeyStatu.Select_Key_Low_Time>=100)
		{
			KeyStatu.Select_Key_Low_Time=0;
			KeyStatu.Select_Key_Press=1;
			KeyStatu.Key_Int=0;
			KeyStatu.BuzzerFlag=1;
			BUZZER_ON;
			BuzzerCnt=0;			
		}	

	}

#if 1	
	if(Sys_Cnt==300)
	{
		Led_Run_Toggle;
	}
	else if(Sys_Cnt==900)
	{

		if(LedStatus.alarm_led==LED_Enable)
		{
			Led_Alarm_Toggle;
			if(Flag==0)
			{
				BUZZER_ON;
			}
			else
			{
				BUZZER_OFF;
			}
			Flag=!Flag;
		}
		else
		{
			Led_Alarm_OFF;
			
		}

	}

	else if(Sys_Cnt==1200)
	{
		
		if(LedStatus.malfunction_led==LED_Enable)
		{
			Led_Malfunction_Toggle;
		}
		else
		{
			Led_Malfunction_OFF;
		}
	}
	else if(Sys_Cnt==1500)
	{
		Sys_Cnt=0;
		if(LedStatus.missingPhase_led==LED_Enable)
		{
			
			Led_MissPhase_Toggle;
		}
		else
		{
			Led_MissPhase_OFF;
		}
	}

#endif	

	if(KeyStatu.BuzzerFlag==1)
	{
		if(++BuzzerCnt>100)
		{
			KeyStatu.BuzzerFlag=0;
			BuzzerCnt=0;
			BUZZER_OFF;
		}
			
	}
	/*if (Debug_Send_Data_Flag == 1)
	{
		if ((Debug_Send_Data_Value&(0x0001<<Debug_Send_Data_Cnt))>0)
		{
			Debug_TX_GPIO_HIGH;
		}
		else
		{
			Debug_TX_GPIO_LOW;
		}
		if (++Debug_Send_Data_Cnt == 11)
		{
			Debug_Send_Data_Flag = 0;
			Debug_Send_Data_Cnt = 0;
		}
	}*/
  }	
}

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
  	HAL_TIM_IRQHandler(&Tim1Handle);
}




void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(timHandle->Instance==TIM3)
  {
  

  
  
    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**TIM3 GPIO Configuration    
    PB1     ------> TIM3_CH4 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF0_TIM3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  
  
  
  }

}
TIM_HandleTypeDef htim3;

void  BuzzerPWMInitial(void)��48MHZ
{
  
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  htim3.Instance = TIM3;	��ʱ��3
  htim3.Init.Prescaler = 24; ��ʱ����Ƶ	 psc
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP; ���ϼ���ģʽ
  htim3.Init.Period = 540;						�Զ�����ֵ  arr
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
	Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
	Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
	Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
	Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1; ��PWM1
  sConfigOC.Pulse = 540/2;			  ���ñȽ�ֵ����ֵ����������ռ�ձ�   =  �Զ�����ֵ/�Ƚ�ֵ 
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW; ����Ƚϼ���Ϊ��
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)����TIM3ͨ��4
  {
	Error_Handler();
  }
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);����PWMͨ��4  --- ע��Ҫ����ͨ��������ֻ�����ã�����������PWM
  
  HAL_TIM_MspPostInit(&htim3);

  BUZZER_OFF;
  

}



#if 0



void MY_TIM3_Init(u16 arr,u16 psc)  �����ɵĺ����Ŀղ�������Ϊ������ֵarr����Ƶϵ��psc��Ϊ������
{
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  htim3.Instance = TIM3;	��ʱ��3
  htim3.Init.Prescaler = psc; ��ʱ����Ƶ   psc
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP; ���ϼ���ģʽ
  htim3.Init.Period = arr;						�Զ�����ֵ  arr
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1; ��PWM1
  sConfigOC.Pulse = arr/2;			  ���ñȽ�ֵ����ֵ����������ռ�ձ�   =  �Զ�����ֵ/�Ƚ�ֵ 
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW; ����Ƚϼ���Ϊ��
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)����TIM3ͨ��4
  {
    Error_Handler();
  }
  HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);����PWMͨ��4  --- ע��Ҫ����ͨ��������ֻ�����ã�����������PWM
  
  HAL_TIM_MspPostInit(&htim3);
  

}





void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM3)
  {
  

  
    
    __HAL_RCC_TIM3_CLK_ENABLE();
  

  
  }
}
#endif 

